<?php
//DB Created by Rich L. a member in service in Northern Delaware.  Please feel free to share this code wherever it may be useful to alcholoics.  Should you have questons please contact me at sobernerd@gmail.com
session_start();

//Sedurity Check, is a valid user logged in? AND if not then display error message
if ($_SESSION['logged_in'] == 0) 
	{
		require ('security_check.php');
	} 
else
	{
// set header assume new Meeting and change below if not
	$pageHeader = 'New Meeting';
$returnTo = $_GET['return_to'];
if ($_GET['group_id']){$return_to_group_id = '&group_id='.$_GET['group_id'];}
		// Get form information if editing
		$id = $_GET['id']; 
		if ($id) 
		{	$id_url_var = '&return_id='.$id;
			$pageHeader = 'Meeting Edit';
			$meetings_sql = "SELECT * FROM meetings WHERE id='".$_GET['id']."'";
					
			$meetings_result =  mysql_query($meetings_sql)
			or die(mysql_error());
			
			// Get each row of data on each iteration until
			// there is no more rows
			while ($row = mysql_fetch_assoc($meetings_result)) {
				$id = $row['id'];
				$day = $row['day'];
				$mtime = $row['mtime'];
				$location_id = $row['location_id'];
				$group_id=$row['group_id'];
				$mtg_type_id = $row['mtg_type_id'];
				$open_closed = $row['open_closed'];
				$men_women = $row['men_women'];
				$room = $row['room'];
				$hc = $row['hc'];
				$lasteditor = $row['editor'];	
				$editdate = $row['editdate'];
													  }
		  }
		  if($_GET['newgroup']){
			  $_SESSION['message'] = '';
			  $groups_sql = "SELECT * FROM groups ORDER BY id DESC LIMIT 1";
			  $groups_result =  mysql_query($groups_sql)
			or die(mysql_error());
				while ($row = mysql_fetch_assoc($groups_result)) {
					$group_id = $row['id'];
				}
			  }
?>

<form id="meetingssform" action="processTables.php?table=meetings&returnTo=<?php echo $returnTo.$return_to_group_id?>" method="post" name="meetingsform">
  <table class="form-table">
    <thead class="meetingHeader">
      <tr class="titleRow">
        <th colspan="9"><h3><?php echo $pageHeader ?></h3></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="labelRight"><label>Day:</label></td>
        <td colspan="2"><select name="day" size="1">
            <option <?php if($day==1){echo "selected =\"selected\"";} ?>='<?php if($day==1){echo "selected =\"selected\"";} ?>' value="1">Sunday</option>
            <option <?php if($day==2){echo "selected=\"selected\"";} ?>='<?php if($day==2){echo "selected=\"selected\"";} ?>' value="2">Monday</option>
            <option <?php if($day==3){echo "selected=\"selected\"";} ?>='<?php if($day==3){echo "selected=\"selected\"";} ?>' value="3">Tuesday</option>
            <option <?php if($day==4){echo "selected=\"selected\"";} ?>='<?php if($day==4){echo "selected=\"selected\"";} ?>' value="4">Wednesday</option>
            <option <?php if($day==5){echo "selected=\"selected\"";} ?>='<?php if($day==5){echo "selected=\"selected\"";} ?>' value="5">Thursday</option>
            <option <?php if($day==6){echo "selected=\"selected\"";} ?>='<?php if($day==6){echo "selected=\"selected\"";} ?>' value="6">Friday</option>
            <option <?php if($day==7){echo "selected=\"selected\"";} ?>='<?php if($day==7){echo "selected=\"selected\"";} ?>' value="7">Saturday</option>
          </select></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Time:</label>
          </div></td>
        <td colspan="2"><select name="mtime" size="1">
            <option value="">None</option>
            <?php 
											//this section generates meeting times, currently 7AM ($hour = 7) to 11PM ($endtime = mktime(23, 15))
											//note, had to add 15 mins to end time to get while loop logic to end at 11 instead of 10:45
											$hour = 7;
											$min = 0;
											$startime = mktime($hour, $min, 0);
											$endtime = mktime(23, 15, 0);
											$nexttime = $startime;
											While ($startime != $endtime){
											$selecttime = date('h:iA',$nexttime);
											$valuetime = date('H:i:s',$nexttime);
											echo "<option ";
											if ($mtime==$valuetime){
												echo "selected=\"selected\"";}
											echo  "value=\"$valuetime\">$selecttime</option>";
											if ($min == 45){
												$min = 0;
												$hour = $hour + 1;
														}else{
												$min = $min + 15;	
															}	
											$nexttime= mktime($hour, $min, 0);
											$startime=$nexttime;
																			}
										
											?>
          </select></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Group Name:</label></td>
        <td><select name="group_id" size="1">
            <option value="0">None Selected</option>
            <?php /* group query for drop down list */
											$groups_sql = "SELECT id, groupname
													FROM groups ORDER BY groupname";
												$groups_result = mysql_query($groups_sql)
												or die(mysql_error());
												while ($groups_row = mysql_fetch_assoc($groups_result)) {
												$groups_id="$groups_row[id]"; //set variable from groups query to compare to meetings query $group_id variable
												echo "<option ";
												//select the name in the drop down box
												if ($groups_id==$group_id){
												echo "selected=\"selected\"";}
												//populate the list
												echo "value=\"$groups_id\">$groups_row[groupname]</option>";
												}?>
          </select></td>
        <td><a href="meetings.php?page=group_edit&return_to=meetings.php?page=meeting_edit<?php echo $id_url_var?>&original_page=<?php echo $returnTo?>" class="groovybutton">Add a Group</a></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Meeting Type:</label></td>
        <td colspan="2">Open
          <input type="radio" name="open_closed" value="Open" <?php if (stristr($open_closed, "Open")) {echo "checked=\"checked\"";}?>='<?php if (stristr($open_closed, "Open")) {echo "checked=\"checked\"";}?>' />
          Closed
          <input type="radio" name="open_closed" value="Closed" <?php if (stristr($open_closed, "Closed")) {echo "checked=\"checked\"";}?>='<?php if (stristr($open_closed, "Closed")) {echo "checked=\"checked\"";}?>' />
          <select name="mtg_type_id" class="meetTypeSelect" size="1">
            <option value="0">None Selected</option>
            <?php /* meeting type query for drop down list */
											$meeting_types_sql = "SELECT 	*
													FROM meeting_types ORDER BY code";
												$meeting_types_result = mysql_query($meeting_types_sql)
												or die(mysql_error());
												
												while ($meeting_types_row = mysql_fetch_assoc($meeting_types_result))
												//set type as variable for this query to slect match
												{$mtg_types_id=$meeting_types_row['id'];
																				
												echo "<option ";
												//select the appproriate name in the drop down box
												if ($mtg_types_id==$mtg_type_id){
												echo "selected=\"selected\"";}
												echo "value=\"$mtg_types_id\">$meeting_types_row[description]</option>";
												}?>
          </select></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Gender:</label></td>
        <td colspan="2">Men
          <input type="radio" name="men_women" value="Men" <?php if (stristr($men_women, "Men")) {echo "checked=\"checked\"";}?>='<?php if (stristr($men_women, "Men")) {echo "checked=\"checked\"";}?>' />
          Women
          <input type="radio" name="men_women" value="Women" <?php if (stristr($men_women, "Women")) {echo "checked=\"checked\"";}?>='<?php if (stristr($men_women, "Women")) {echo "checked=\"checked\"";}?>' />
            
          Both
          <input type="radio" name="men_women" value="Both" <?php if (stristr($men_women, "Both")) {echo "checked=\"checked\"";}?>='<?php if (stristr($men_women, "Both")) {echo "checked=\"checked\"";}?>' /></td>
      </tr>
      <tr>
        <td class="labelRight"><label>HC Accessible:</label></td>
        <td colspan="2">Yes
          <input type="radio" name="hc" value="Y" <?php if (stristr($hc, "Y")) {echo "checked=\"checked\"";}?>='<?php if (stristr($hc, "Y")) {echo "checked=\"checked\"";}?>' />
             
          No
          <input type="radio" name="hc" value="N" <?php if (stristr($hc, "N")) {echo "checked=\"checked\"";}?>='<?php if (stristr($hc, "N")) {echo "checked=\"checked\"";}?>' /></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Location:</label></td>
        <td colspan="2"><select name="location_id" size="1">
            <option value="0">None Selected</option>
            <?php /* location query for drop down list */
											$locs_sql = "SELECT id, name
													FROM locations ORDER BY name";
													
												$locs_result = mysql_query($locs_sql)
												or die(mysql_error());
												
												while ($locs_row = mysql_fetch_assoc($locs_result))
												{$locations_id="$locs_row[id]"; //set variable from location query to compare to meetings query $location_id variable
												echo "<option ";
												//select the appproriate name in the drop down box
												if ($locations_id==$location_id){
												echo "selected=\"selected\"";}
												//populate the list
												echo "value=\"$locations_id\">$locs_row[name]</option>";
												}?>
          </select></td>
      </tr>
      <tr>
        <td class="labelRight"><label>Room or Directions:</label></td>
        <td colspan="2"><input type="text" name="room" value="<?php echo($room)?>" size="24" /></td>
      </tr>
      <tr>
        <td colspan="3" align="center"><input type="submit" name="submit" value="Submit" class="groovybutton" />
          <input type="button" value="Cancel" class="groovybutton" onClick="history.back()"></td>
      </tr>
    </tbody>
  </table>
  <p>
    <?php
	//if admin display last editor and date
	if ($_SESSION['admin'] == 1) {
	/* format date for display */
				$current_editdate = strtotime($editdate);
				$displaydate = date('m/d/Y g:i:s A', $current_editdate);							
	echo "<table class = \"admintable\" width=\"500px\" border=\"0\" cellspacing=\"2\" cellpadding=\"0\">
		<tr>
			<td align =\"right\"><strong>Last Editor:</strong></td><td>$lasteditor</td>
			<td align =\"right\"><strong>Last Edit Date/Time:</strong></td><td>$displaydate</td>
		</tr>
		</table>";  } ?>
    <input type="hidden" name="editor" value="<?php echo $_SESSION['username'] ?>" />
    <input type="hidden" name="id" value="<?php echo $id ?>" />
  </p>
</form>
<?php }
mysql_close($conn); ?>
